#ifndef _SQLITE_H_
#define _SQLITE_H_

#include "lib/sqlite3/sqlite3.h"

#include<ostream>
#include<string>

// table model
/*
class Localize( db.Model ) :
  id = db.Column( db.Integer, primary_key = True )
  state = db.Column( db.Integer )
  featfile = db.Column( db.String( 120 ) )
  descfile = db.Column( db.String( 120 ) )
  imgwidth = db.Column( db.Integer )
  imgheight = db.Column( db.Integer )
  localizeokay = db.Column( db.Boolean )
  time = db.Column( db.Float )
  posx = db.Column( db.Float )
  posy = db.Column( db.Float )
  posz = db.Column( db.Float )
  quatw = db.Column( db.Float )
  quatx = db.Column( db.Float )
  quaty = db.Column( db.Float )
  quatz = db.Column( db.Float ) 
*/


namespace SQL {
 
  struct Localize
  {
    Localize() {
      id = state = imgwidth = imgheight = 0;
      time = posx = posy = posz = quatw = quatx = quaty = quatz = 0.0f;
      localizeokay = false;
      featfile.clear();
      descfile.clear();
    }
    int id, state, imgwidth, imgheight;
    float time, posx, posy, posz, quatw, quatx, quaty, quatz;
    bool localizeokay;
    std::string  featfile, descfile;
  };

  class SqliteHandler
  {
  public:
    
    SqliteHandler( const std::string& db_root, const std::string& db ) : db_root( db_root ), db_filename( db ) {};
    ~SqliteHandler() { sqlite3_close( db ); };

    // open sqlite database file
    bool open();

    // find possible query picture and get the id value
    bool FindQueryPicture( Localize& localize_sql_model );

    // write localize result into table
    bool WriteLocalizeResult( const Localize& localize_sql_model );

  private:
    static int SqliteHandler::FindCallback( void *data, int argc, char **argv, char **azColName );

    std::string db_root;
    std::string db_filename;
    sqlite3 *db;   
  };
};

std::ostream& operator<<( std::ostream& stream, const SQL::Localize& localize );


#endif
